<template>
  <el-cascader
    :options="options"
    :props="{ value: 'id',label: 'name' }"
    size="mini"
    :value="value"
    separator="-"
    @change="changeValue"
  />
</template>

<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      id: [],
      options: []
    }
  },
  async created() {
    const res = await getDepartment()
    this.options = transListToTreeData(res.data.data, 0)
  },
  methods: {
    changeValue(list) {
      // 取到数组的最后一次
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1]) // 将最后一位的id取出来 传出去
      } else {
        this.$emit('input', null) // 如果长度为0 说明值为空
      }
    }
  }
}
</script>
